package cn.max.controllers;

import cn.incontent.fastjson.JSONObject;
import cn.max.SessionCleaner;
import cn.max.web.listeners.DBInitiator;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *@author Val.(Valentine Vincent) E-mail:valer@126.com
 *@version 1.0
 *@date 2013-12-25
 *Instruction : 
 **/
@Controller
public class AuthenticationController {
	
	private static final long serialVersionUID = 1L;

	@RequestMapping("ls/authentication/login")
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		//start to login
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		
		SqlSession session = DBInitiator.getSqlSession();
		
//		ClientTokenMapper ctm = session.getMapper(ClientTokenMapper.class);
//		ClientToken user = ctm.selectByPrimaryKey(userName);
//
		JSONObject res = new JSONObject();
//
//		if (user != null && password.equals(user.getToken())) {
//
//			request.getSession().setAttribute(SystemConstants.USER_NAME, userName);
//			request.getSession().setAttribute(SystemConstants.PWD, password);
//			request.getSession().setAttribute(SystemConstants.TOKEN, user.getToken());
//			request.getSession().setAttribute(SystemConstants.USER_CODE, user.getClientCode());
//			request.getSession().setAttribute(SystemConstants.TIMESTAMP, new Date().getTime());
//			request.getSession().setAttribute(SystemConstants.USER, user);
//
//			res.put("success", true);
//		} else {
//			res.put("success", false);
//		}
		
		response.setContentType("text/html; charset=utf-8");
		response.getWriter().write(res.toString());
		
		return null;
	}
	
	@RequestMapping("ls/authentication/logout")
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		SessionCleaner.clearSession(request);

		JSONObject res = new JSONObject();
		res.put("success", true);
		
		response.setContentType("text/html; charset=utf-8");
		response.getWriter().write(res.toString());

		return null;
	}
	
}